package leetcode.周赛.第276场;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/16 14:03
 * @description：https://leetcode-cn.com/problems/divide-a-string-into-groups-of-size-k/
 */
public class 将字符串拆分为若干长度为k的组 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.divideString("abcdefg", 3, 'x')));
    }

    class Solution {
        public String[] divideString(String s, int k, char fill) {
            int n = s.length() / k;
            int factor = s.length() % k;
            String[] ans = new String[n + (factor == 0 ? 0 : 1)];
            for (int i = 0; i < n; i++) {
                ans[i] = s.substring(i * k, (i + 1) * k);
            }
            if (factor == 0) return ans;
            StringBuilder last = new StringBuilder(s.substring(n * k));
            while (last.length() < k) {
                last.append(fill);
            }
            ans[n] = last.toString();
            return ans;
        }
    }
}

